package com.superman.supermarket.client.view.preorder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.supermarket.client.composite.IUIModule;
import com.superman.supermarket.client.composite.preorder.PreorderStoreModule;
import com.superman.supermarket.client.composite.preorder.addr.PreorderAddrModule;
import com.superman.supermarket.client.composite.preorder.info.PreorderOrderInfoModule;

/**
 * 确认订单ui实现类
 * 
 * @author superman
 * @version 2018年8月2日下午5:29:57
 */
public class PreorderViewImpl extends Composite implements PreorderView {

	interface PreorderViewImplUiBinder extends UiBinder<Widget, PreorderViewImpl> {
	}

	private static PreorderViewImplUiBinder uiBinder = GWT.create(PreorderViewImplUiBinder.class);

	@UiField
	PreorderAddrModule addrinfomodule;
	@UiField
	PreorderOrderInfoModule infomodule;

	@UiField
	FlowPanel newaddrpanel, storelistpanel;
	@UiField
	HTML payfee2html;
	private PreorderViewPresenter presenter;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月6日 下午10:17:40
	 */
	public PreorderViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));

	}

	@Override
	public String getCarryMode() {
		return infomodule.getCarryMode();
	}

	@Override
	public List<Map<String, Object>> getStoreSkuList() {
		List<Map<String, Object>> storeSkuList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < storelistpanel.getWidgetCount(); i++) {
			Widget widget = storelistpanel.getWidget(i);
			if (widget instanceof IUIModule) {
				IUIModule uiModule = (IUIModule) widget;
				storeSkuList.add(uiModule.getData());

			}
		}
		return storeSkuList;
	}

	@Override
	public float getTotalFee() {
		return infomodule.getTotalFee();
	}

	@Override
	public void init() {

	}

	@UiHandler("newaddrbtn")
	void newAddr(ClickEvent event) {
		presenter.pushToChooseAddr();
	}

	@UiHandler("submitbtn")
	void pay(ClickEvent event) {
		presenter.addOrder();
	}

	@Override
	public void setAddrInfo(Map<String, Object> map) {
		addrinfomodule.setVisible(true);
		newaddrpanel.setVisible(false);

		addrinfomodule.setData(map);
	}

	@Override
	public void setNoAddrInfo() {
		addrinfomodule.setVisible(false);
		newaddrpanel.setVisible(true);
	}

	@Override
	public void setOrderInfo(Map<String, Object> map) {
		infomodule.setData(map);
		payfee2html.setHTML("实付款:&nbsp;&nbsp;&nbsp;&nbsp;¥&nbsp;" + infomodule.getPayFee());

	}

	@Override
	public void setPageViewPresenter(IPageViewPresenter presenter) {
		this.presenter = (PreorderViewPresenter) presenter;
	}

	@Override
	public void setStoreList(List<Map<String, Object>> list) {
		storelistpanel.clear();
		for (Map<String, Object> map : list) {
			PreorderStoreModule preorderStoreModule = new PreorderStoreModule();
			preorderStoreModule.setData(map);
			storelistpanel.add(preorderStoreModule);
		}
	}

}
